<?php

namespace App\Services;

use App\Models\Area;
use App\Models\Good;
use App\Models\GoodVisit;
use App\Models\ShopUser;
use App\Models\User;

class GoodService
{
    /**
     * 查询商品列表
     * @return array
     */
    public static function getList(): array
    {
        $pageSize = request()->input("pageSize",10);
        $category_id = request()->input("category_id",0);
        $where = ['status'=>0];
        if ($category_id){
            $where['category_id'] = $category_id;
        }
        $list = Good::query()->select(["id","name","image","price"])->where($where)->paginate($pageSize);
        $items = collect($list->items())->toArray();
        foreach ($items as $key=>$item){
            $items[$key]['image'] = cdn_url($item['image']);
        }
        $data['items'] = $items;
        $data['total'] = $list->total();
        $data['lastPage'] = $list->lastPage();
        return $data;
    }



    /**
     * 查询商品详情
     * @param $id
     * @param int $user_id
     * @return mixed
     */
    public static function getDetail($id, int $user_id = 0)
    {
        $goods = Good::whereId($id)->with(['category'=>function($query){
            $query->select(['id','name']);
        }])->withExists(["collect"=>function($query) use($user_id) {
            $query->where("user_id",$user_id);
        }])->first();
        if ($goods){
            $images = $goods->images&&is_string($goods->images)?json_decode($goods->images,true):$goods->images;
            $goods->image = cdn_url($goods->image);
            $goods->images = cdn_url($images);
            if ($user_id){
                GoodVisit::addVisit($id, $user_id);
            }
        }
        return $goods;
    }


    public static function handleCars($params = [],$user_id = 0): array
    {
        $address = $params['address'];
        $province_id = 0;
        $city_id = 0;
        $district_id = 0;
        if ($params['method']){
            $shop_id = $address['shop_id'];
            $shop = ShopUser::find($shop_id);
            $province_id = $shop->province_id;
            $city_id = $shop->city_id;
            $district_id = $shop->district_id;
            $address['name']= $shop->name;
            $address['address']= $shop->address;
            $address['phone']= $shop->phone;
            $ids = [$province_id,$city_id,$district_id];
            $area = Area::whereIn("id",$ids)->pluck("name")->toArray();
            $address['area']= implode("/",$area);
        }else{
            $address['address'] = str_replace("/","",$address['area']).$address['address'];
            $address['shop_id'] = 0;
            $area = explode("/",$address['area']);
            $area[0] = rtrim($area[0],"市");
            $province = Area::where("name",$area[0])->where("pid",0)->first();
            if ($province){
                $province_id = $province->id;
                $city = Area::where("name",$area[1])->where("pid",$province_id)->first();
                if ($city){
                    $city_id = $city->id;
                    $district = Area::where("name",$area[2])->where("pid",$city_id)->first();
                    if ($district){
                        $district_id = $district->id;
                    }
                }
            }
        }
        $params['province_id'] = $province_id;
        $params['city_id'] = $city_id;
        $params['district_id'] = $district_id;
        $params['user_id'] = $user_id;
        $params['address'] = $address;
        $user = User::query()->where("id",$user_id)->first();
        foreach ($params['cars'] as $k=>$car){
            $good_id = $car['good_id'];
            $good = Good::find($good_id);
            $good_number = $car['good_number'];
            $good_price = $good->price;
            $params['cars'][$k]['good_name'] = $good->name;
            $params['cars'][$k]['good_image'] = $good->image;
            $params['cars'][$k]['good_price'] = $good_price;
            $total_price = $good_price * $good_number;
            $params['cars'][$k]['total_price'] = $total_price;
            $params['cars'][$k]['shop_rate'] = $good->shop_rate;
            $user_rate = 0;
            if ($user&&$user->tid){
                $user_rate = $good->user_rate;
            }
            $params['cars'][$k]['user_rate'] = $good->user_rate;
            $params['cars'][$k]['user_money'] = ($user_rate/100) * $total_price;
            $params['cars'][$k]['shop_money'] = ($good->shop_rate/100) * $total_price;
            $params['cars'][$k]['agent_city_rate'] = $good->city_rate0;
            $params['cars'][$k]['agent_city_money'] = ($good->city_rate0/100)*$total_price;
            $params['cars'][$k]['agent_city_rate1'] = $good->city_rate1;
            $params['cars'][$k]['agent_city_money1'] = ($good->city_rate1/100)*$total_price;
            $params['cars'][$k]['agent_district_rate'] = $good->district_rate;
            $params['cars'][$k]['agent_district_money'] = ($good->district_rate/100)*$total_price;
        }
        return $params;
    }


}
